package equipment_kind

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/equipment_kind"
	equipment_kindReq "github.com/flipped-aurora/gin-vue-admin/server/model/equipment_kind/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type EquipmentKindApi struct {
}

var equipmentKindService = service.ServiceGroupApp.EquipmentKindServiceGroup.EquipmentKindService

// CreateEquipmentKind 创建equipmentKind表
// @Tags EquipmentKind
// @Summary 创建equipmentKind表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body equipment_kind.EquipmentKind true "创建equipmentKind表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /equipmentKind/createEquipmentKind [post]
func (equipmentKindApi *EquipmentKindApi) CreateEquipmentKind(c *gin.Context) {
	var equipmentKind equipment_kind.EquipmentKind
	err := c.ShouldBindJSON(&equipmentKind)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := equipmentKindService.CreateEquipmentKind(&equipmentKind); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteEquipmentKind 删除equipmentKind表
// @Tags EquipmentKind
// @Summary 删除equipmentKind表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body equipment_kind.EquipmentKind true "删除equipmentKind表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /equipmentKind/deleteEquipmentKind [delete]
func (equipmentKindApi *EquipmentKindApi) DeleteEquipmentKind(c *gin.Context) {
	var equipmentKind equipment_kind.EquipmentKind
	err := c.ShouldBindJSON(&equipmentKind)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := equipmentKindService.DeleteEquipmentKind(equipmentKind); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteEquipmentKindByIds 批量删除equipmentKind表
// @Tags EquipmentKind
// @Summary 批量删除equipmentKind表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除equipmentKind表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /equipmentKind/deleteEquipmentKindByIds [delete]
func (equipmentKindApi *EquipmentKindApi) DeleteEquipmentKindByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := equipmentKindService.DeleteEquipmentKindByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateEquipmentKind 更新equipmentKind表
// @Tags EquipmentKind
// @Summary 更新equipmentKind表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body equipment_kind.EquipmentKind true "更新equipmentKind表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /equipmentKind/updateEquipmentKind [put]
func (equipmentKindApi *EquipmentKindApi) UpdateEquipmentKind(c *gin.Context) {
	var equipmentKind equipment_kind.EquipmentKind
	err := c.ShouldBindJSON(&equipmentKind)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := equipmentKindService.UpdateEquipmentKind(equipmentKind); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindEquipmentKind 用id查询equipmentKind表
// @Tags EquipmentKind
// @Summary 用id查询equipmentKind表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query equipment_kind.EquipmentKind true "用id查询equipmentKind表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /equipmentKind/findEquipmentKind [get]
func (equipmentKindApi *EquipmentKindApi) FindEquipmentKind(c *gin.Context) {
	var equipmentKind equipment_kind.EquipmentKind
	err := c.ShouldBindQuery(&equipmentKind)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reequipmentKind, err := equipmentKindService.GetEquipmentKind(equipmentKind.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reequipmentKind": reequipmentKind}, c)
	}
}

// GetEquipmentKindList 分页获取equipmentKind表列表
// @Tags EquipmentKind
// @Summary 分页获取equipmentKind表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query equipment_kindReq.EquipmentKindSearch true "分页获取equipmentKind表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /equipmentKind/getEquipmentKindList [get]
func (equipmentKindApi *EquipmentKindApi) GetEquipmentKindList(c *gin.Context) {
	var pageInfo equipment_kindReq.EquipmentKindSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := equipmentKindService.GetEquipmentKindInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (equipmentKindApi *EquipmentKindApi) GetEquipmentKindListNo(c *gin.Context) {
	var pageInfo equipment_kindReq.EquipmentKindSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	data, err := equipmentKindService.GetEquipmentKindInfo()
	if err != nil {
		fmt.Println(err)
		return
	}

	c.JSON(0, gin.H{
		"code": 0,
		"msg":  "获取成功",
		"data": data,
	})

}
